package mortal.game.control
{
    import Message.Game.*;
    import Message.Public.*;
    import modules.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.view.system.*;
    import mortal.game.view.treasure.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class TreasureController extends Controller
    {
        private var _treasureModule:TreasureModule;
        private var _isExplorer:Boolean = false;

        public function TreasureController()
        {
            
        }

        override protected function initView() : IView
        {
            if (this._treasureModule == null)
            {
                this._treasureModule = new TreasureModule();
            }
            return this._treasureModule;
        }

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.TreasureExplorer, this.onTreasureExplorerHandler);
            Dispatcher.addEventListener(EventName.TreasureGetExplorerRecords, this.onGetExplorerRecordsHandler);
            Dispatcher.addEventListener(EventName.TreasureModuleOpen, this.onTreasureModuleOpenHandler);
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.onMoneyUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackTreasureGetItemsChange, this.onTreasurePackChangeHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackTreasurePackChange, this.onTreasurePackChangeHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackTreasureGetItemsChange, this.onTreasureGetItemsChangeHandler);
            Dispatcher.addEventListener(EventName.TreasureGetRecordsSuccess, this.onGetRecordsSuccessHandler);
            Dispatcher.addEventListener(EventName.TreasureExplorerRumorAdd, this.onExplorerRumorAddHandler);
            
        }

        private function onTreasureExplorerHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data;
            GameProxy.sceneProxy.explorer(_loc_2.type, _loc_2.amount);
            if (!GameController.treasureWarehourse.treasureGetItem.isHide)
            {
                GameController.treasureWarehourse.treasureGetItem.hide();
            }
            this._isExplorer = true;
            
        }

        private function onGetExplorerRecordsHandler(event:DataEvent) : void
        {
            GameProxy.sceneProxy.getExplorerRecords();
            
        }

        private function onTreasureModuleOpenHandler(event:DataEvent) : void
        {
            if (event.data == null)
            {
                if (view.isHide)
                {
                    view.show();
                    this.judeShowGuideArrow();
                }
                else
                {
                    view.hide();
                }
            }
            else if (view.isHide)
            {
                view.show();
                this.judeShowGuideArrow();
            }
            
        }

        private function judeShowGuideArrow() : void
        {
            if (cache.pack.treasurePackCache.getItemsByJudge().length > 0)
            {
                if (!SystemSetter.localSetter.isGuideOpen)
                {
                    this._treasureModule.showGuideArrow();
                }
            }
            
        }

        private function onMoneyUpdateHandler(param1:Object) : void
        {
            var _loc_2:* = param1 as SMoney;
            if (this._treasureModule != null)
            {
                this._treasureModule.updateMoney(_loc_2.gold);
            }
            
        }

        private function onTreasureGetItemsChangeHandler(param1:Object) : void
        {
            var _loc_2:* = param1;
            if (this._treasureModule != null && this._isExplorer)
            {
                this._isExplorer = false;
                this._treasureModule.update(_loc_2);
                this.judeShowGuideArrow();
            }
            
        }

        private function onTreasurePackChangeHandler(param1:Object) : void
        {
            if (this._treasureModule != null)
            {
                this._treasureModule.updateCapacity();
            }
            
        }

        private function onGetRecordsSuccessHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as Array;
            ExplorerRumorData.noticeArr = _loc_2;
            if (this._treasureModule != null)
            {
                this._treasureModule.updateExplorerRecords();
            }
            
        }

        private function onExplorerRumorAddHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SPublicNotice;
            ExplorerRumorData.setNoticeArr([_loc_2]);
            if (this._treasureModule != null)
            {
                this._treasureModule.updateExplorerRecords();
            }
            
        }

    }
}
